// pages/tools/yunqian/jbxx.js
var _$ = require("../../../common/wxjs/wx_ajax");
const url = "yq_jkjc";

Page({

    /**
     * 页面的初始数据
     */
    data: {
        selected: 0,
        statuearry: ['正常', '异常'],
        ABOarry: ['A型', 'B型', 'O型', 'AB型'],
        RHarry: ['不祥', '阳性', '阴性'],
        uesrtoken: null,
        formdata: {
            // 母亲按钮
            "m_ssy": '',
            "m_szy": '',
            "m_xhdb": '',
            "m_xxb": '',
            "m_kfxt": '',
            "m_gxy": 0,
            "m_tnb": 0,
            "m_ggn": 0,
            "m_sgn": 0,
            "m_jzxjs": 0,
            "m_ydb": 0,
            "m_dcgr": 0,
            "m_lqjgr": 0,
            "m_jsjmj": 0,
            "m_syyyt": 0,
            "m_yxgy": 0,
            "m_mdlxt": 0,
            "m_hivkt": 0,
            "m_flggkt": 0,
            "m_jlgmkt": 0,
            "m_jlggkt": 0,
            "m_glgmkt": 0,
            "m_glggkt": 0,
            "m_szxt": 0,
            "m_abo": 0,
            "m_rh": 0,
            // 父亲按钮
            "f_ssy": '',
            "f_szy": '',
            "f_gxy": 0,
            "f_tnb": 0,
            "f_ggn": 0,
            "f_sgn": 0,
            "f_gybm": 0,
            "f_mdlx": 0,
            "f_hiv": 0,
            "f_szxt": 0,
            "f_abo": 0,
            "f_rh": 0,
        },

    },
    // 按钮切换
    btnselect: function(e) {
        var that = this;
        that.setData({
            // 母亲数据
            "formdata.m_gxy": e.target.dataset.m_gxy,
            "formdata.m_tnb": e.target.dataset.m_tnb,
            "formdata.m_ggn": e.target.dataset.m_ggn,
            "formdata.m_sgn": e.target.dataset.m_sgn,
            "formdata.m_jzxjs": e.target.dataset.m_jzxjs,
            "formdata.m_ydb": e.target.dataset.m_ydb,
            "formdata.m_dcgr": e.target.dataset.m_dcgr,
            "formdata.m_lqjgr": e.target.dataset.m_lqjgr,
            "formdata.m_jsjmj": e.target.dataset.m_jsjmj,
            "formdata.m_syyyt": e.target.dataset.m_syyyt,
            "formdata.m_yxgy": e.target.dataset.m_yxgy,
            "formdata.m_mdlxt": e.target.dataset.m_mdlxt,
            "formdata.m_hivkt": e.target.dataset.m_hivkt,
            "formdata.m_flggkt": e.target.dataset.m_flggkt,
            "formdata.m_jlgmkt": e.target.dataset.m_jlgmkt,
            "formdata.m_jlggkt": e.target.dataset.m_jlggkt,
            "formdata.m_glgmkt": e.target.dataset.m_glgmkt,
            "formdata.m_glggkt": e.target.dataset.m_glggkt,
            // 父亲数据
            "formdata.f_gxy": e.target.dataset.f_gxy,
            "formdata.f_tnb": e.target.dataset.f_tnb,
            "formdata.f_ggn": e.target.dataset.f_ggn,
            "formdata.f_sgn": e.target.dataset.f_sgn,
            "formdata.f_gybm": e.target.dataset.f_gybm,
            "formdata.f_mdlx": e.target.dataset.f_mdlx,
            "formdata.f_hiv": e.target.dataset.f_hiv,
        })
    },
    mszxtChange: function(e) {
        this.setData({
            "formdata.m_szxt": e.detail.value,

        })
    },
    msaboChange: function(e) {
        this.setData({
            "formdata.m_abo": e.detail.value,

        })
    },
    msrhChange: function(e) {
        this.setData({
            "formdata.m_rh": e.detail.value,

        })
    },
    fszxtChange: function(e) {
        this.setData({
            "formdata.f_szxt": e.detail.value,

        })
    },
    fsaboChange: function(e) {
        this.setData({
            "formdata.f_abo": e.detail.value,

        })
    },
    fsrhChange: function(e) {
        this.setData({
            "formdata.f_rh": e.detail.value,

        })
    },
    // 提交表单
    formSubmit: function(e) {
        var that = this;
        var userdata = e.detail.value;
        userdata = JSON.stringify(userdata);
        var litToken = that.data.uesrtoken;
        var type = 1;
        var methods = "POST";
        var header = "application/x-www-form-urlencoded";
        var dataobj = { type, userdata,litToken };
        wx.showToast({
            title: '正在提交',
            icon: 'loading',
            duration: 2000
        })
        _$.sendRrquest(url, methods, dataobj, header)
            .then(function(response) {
              console.log(response)
                if (response.data.status == 200 ) {
                    wx.showToast({
                        title: '提交成功',
                        icon: 'success',
                        duration: 2000
                    });
                    setTimeout(function() {
                        wx.hideToast()
                    }, 2000)
                } else {
                    wx.showToast({
                        title: '数据未修改',
                        icon: 'loading',
                        duration: 2000
                    });
                    setTimeout(function() {
                        wx.hideToast()
                    }, 2000)
                }
            }, function(error) {
                wx.showToast({
                    title: '提交失败',
                    duration: 2000
                });
                setTimeout(function() {
                    wx.hideToast()
                }, 2000)
                console.log(error);
            });
    },
    // 请求数据
    getdata: function(val) {
        var that = this;
        var type = 0;
        var litToken = val;
        var dataobj = { type, litToken };
        var methods = "GET";
        var header = "application/json";
        wx.showLoading({
            title: '加载中',
        })
        _$.sendRrquest(url, methods, dataobj, header)
            .then(function(response) {
                setTimeout(function() {
                    wx.hideLoading()
                }, 1000)
                console.log(response)
                that.setData({
                    formdata: response.data.data.res
                });
            }, function(error) {
                setTimeout(function() {
                    wx.hideLoading()
                }, 2000)
                wx.showToast({
                    title: '网络错误，请稍后再试',
                    duration: 2000
                })
                setTimeout(function() {
                    wx.hideToast()
                }, 2000)
                console.log(error);
            });
    },
    /**
     * 生命周期函数--监听页面加载
     */
    onLoad: function(options) {
        var that = this
      wx.getStorage({
        key: 'token',
        success: function (res) {
          that.setData({
            uesrtoken: res.data
          })
          that.getdata(res.data)
        }
      })
    },

    /**
     * 生命周期函数--监听页面初次渲染完成
     */
    onReady: function() {

    },

    /**
     * 生命周期函数--监听页面显示
     */
    onShow: function() {

    },

    /**
     * 生命周期函数--监听页面隐藏
     */
    onHide: function() {

    },

    /**
     * 生命周期函数--监听页面卸载
     */
    onUnload: function() {

    },

    /**
     * 页面相关事件处理函数--监听用户下拉动作
     */
    onPullDownRefresh: function() {

    },

    /**
     * 页面上拉触底事件的处理函数
     */
    onReachBottom: function() {

    },

    /**
     * 用户点击右上角分享
     */
    onShareAppMessage: function() {

    },
    /** 
     * 点击tab切换 
     */
    swichNav: function(e) {
        var that = this;
        if (this.data.selected === e.target.dataset.selected) {
            return false;
        } else {
            that.setData({
                selected: e.target.dataset.selected
            })
        }
    },

})